package panda.client.ui.widgets;

import panda.client.JSNI;
import panda.client.Cardpanda;
import panda.client.enumerations.CardScale;
import panda.client.enumerations.Color;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

/** A bar of buttons to click on for actions :P */
public class ActionBar extends Composite {
	private HorizontalPanel shell;
	private Button toggleTokenMenu;
	private Button randomNumberGen;
	private Button redCounterButton;
	private Button greenCounterButton;
	private Button blueCounterButton;
	private Button drawOpeningHand;
	private Button mulligan;

	public ActionBar() {
		shell = new HorizontalPanel();

		/* toggle the visibility of the token menu */
		toggleTokenMenu = new Button("Tokens");
		setButtonStyle(toggleTokenMenu.getElement());
		toggleTokenMenu.setTitle("Toggle the token creation dialog.");
		toggleTokenMenu.addClickListener(new ClickListener() {
			TokenSummonDialog tokenMenu = Cardpanda.tokenMenu;

			public void onClick(Widget sender) {
				if(tokenMenu.isOpen()) {
					tokenMenu.hide();
				} else {
					tokenMenu.show();
				}
			}
		});
		
		/* roll a 20 sided die and display the results in NoteTip */
		randomNumberGen = new Button("D20 Roll");
		setButtonStyle(randomNumberGen.getElement());
		randomNumberGen.setTitle("Roll a 20 sided die.");
		randomNumberGen.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				int random = 1 + (int) Math.round(Math.random() * 19);
				
				String logForMe = "You roll a " + random + ".";
				String logForOthers = Cardpanda.clientModel.getSessionID().getName() + " rolls a " + random + ".";
				NoteTip.get().log(logForMe, logForOthers);
				Cardpanda.sendModelUpdateToServer(Cardpanda.clientModel.getSessionID());
			}
		});

		/* red counter */
		redCounterButton = new Button("Counter");
		initAddCounterButton(redCounterButton, Color.RED);

		/* green counter */
		greenCounterButton = new Button("Counter");
		initAddCounterButton(greenCounterButton, Color.GREEN);

		/* blue counter */
		blueCounterButton = new Button("Counter");
		initAddCounterButton(blueCounterButton, Color.BLUE);
		
		/* draw an opening hand of 7 cards */
		drawOpeningHand = new Button("Start");
		setButtonStyle(drawOpeningHand.getElement());
		drawOpeningHand.setTitle("Start the game by drawing 7 cards for your opening hand.");
		drawOpeningHand.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				Cardpanda.clientModel.drawSevenCards();

				String logForMe = "You draw 7 cards.";
				String logForOthers = Cardpanda.clientModel.getSessionID().getName() + " draws 7 cards.";
				NoteTip.get().log(logForMe, logForOthers);
				Cardpanda.sendModelUpdateToServer(Cardpanda.clientModel.getSessionID());
			}
		});
		
		/* mulligan */
		mulligan = new Button("Mulligan");
		setButtonStyle(mulligan.getElement());
		mulligan.setTitle("Draw a new hand, except you draw one less card.");
		mulligan.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				int currentHandSize = Cardpanda.clientModel.Mulligan();
				
				String logForMe = "You mulligan to " + currentHandSize + " cards.";
				String logForOthers = Cardpanda.clientModel.getSessionID().getName() + " mulligans to " + currentHandSize + " cards.";
				NoteTip.get().log(logForMe, logForOthers);
				Cardpanda.sendModelUpdateToServer(Cardpanda.clientModel.getSessionID());
			}
		});

		shell.add(randomNumberGen);
		shell.add(toggleTokenMenu);
		shell.add(redCounterButton);
		shell.add(greenCounterButton);
		shell.add(blueCounterButton);
		shell.add(drawOpeningHand);
		shell.add(mulligan);
		shell.setSpacing(3);
		initWidget(shell);
	}
	
	private void initAddCounterButton(final Button button, final Color color) {
		DOM.setStyleAttribute(button.getElement(), "backgroundColor", color.toString());
		DOM.setStyleAttribute(button.getElement(), "color", "white");
		setButtonStyle(button.getElement());
		button.setTitle("Add a " + color.getName() + " counter to the playing area");
		button.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				Cardpanda.clientModel.createCounter(color, 40 + CardScale.QUARTER.getWidth() / 2, Cardpanda.canvasHeight / 2 - Counter.height / 2);
						
			}
		});
	}

	private void setButtonStyle(Element el) {
		DOM.setStyleAttribute(el, "border", "1px solid black");
		if(JSNI.getBrowserString().contains("msie 6.0") || JSNI.getBrowserString().contains("msie 7.0")) {
			/* don't use the transparent png.  7.0 supports it but it causes 1px padding in buttons =( */
		} else {
			DOM.setStyleAttribute(el, "backgroundImage", "url(" + NoteTip.GradientURL + ")");
			DOM.setStyleAttribute(el, "backgroundPosition", "bottom");
		}
	}

	// public void setStatusText(String txt) {
	// status.setText(txt);
	// }
}